function [ValidTE, ValidET] = test_mesh_unity(V, T, E, ET, TE)
% function isValid = test_mesh_unity(V, T, E, ET, TE)
% this function is used to validate the mesh (V, T, E, ET, TE)

figure; hold on;title('check mesh');
% first check E, locally E(1,:) = [T(:,2) T(:,3)]
% flagE = zeros(size(E,1),1);


% then check ET
% flagET = zeros(size(E,1),1);
bndE = find(ET(:,2)==0);  % correct   for the boundary

inE    = find(ET(:,2)~=0);

TLeft = T(ET(inE, 1),:);
XY = (2*V(TLeft(:,1),:) + V(TLeft(:,2),:) + V(TLeft(:,3),:))/4; 
text(XY(:,1), XY(:,2), num2str(ET(inE, 1))); 

TRight = T(ET(inE, 2),:);
XY = (V(TRight(:,1),:) + 2*V(TRight(:,2),:) + V(TRight(:,3),:))/4; 
text(XY(:,1), XY(:,2), num2str(ET(inE, 2)));

% next, check TE
% flagTE = zeros(size(T,1),1);
EdgeNodes = [E(TE(:,1),:), E(TE(:,2),:), E(TE(:,3),:)];
ValidTE = sum(EdgeNodes,2) - 2*sum(T,2);


% at last, plot the mesh
ZERO = zeros(size(V,1),1);  trisurf(T,V(:,1),V(:,2),ZERO); view(0,90);
hold off;